package com.example.demo.view;

import com.example.demo.dao.AdminDao;
import com.example.demo.model.Admin;
import com.example.demo.util.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings({ "serial", "unused" })
public class LoginFrm extends JFrame {

    private JPanel contentPane;
    private JLabel titleLabel;
    private JLabel anameJlabel;
    private JTextField anameTextField;
    private JLabel passwordJlabel;
    private JPasswordField passwordTextField;
    private JButton submitButton;
    private JButton resetButton;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginFrm frame = new LoginFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public LoginFrm() {
        setTitle("登录系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
        setBounds(500, 150, 630, 450);//设置页面大小
        setResizable(false);//设置页面不可拖拽改变大小
        getContentPane().setLayout(null);
        setVisible(true);

        titleLabel = new JLabel("员工管理系统");
        titleLabel.setFont(new Font("华文行楷", Font.PLAIN, 30));
        titleLabel.setBounds(215, 65, 182, 34);
        getContentPane().add(titleLabel);

        anameJlabel =new JLabel("账号：");
        anameJlabel.setFont(new Font("宋体", Font.PLAIN, 20));
        anameTextField=new JTextField();
        anameJlabel.setBounds(155, 152, 67, 30);
        anameTextField.setBounds(243, 152, 200, 30);
        getContentPane().add(anameJlabel);
        getContentPane().add(anameTextField);

        passwordJlabel =new JLabel("密码：");
        passwordJlabel.setFont(new Font("宋体", Font.PLAIN, 20));
        passwordTextField=new JPasswordField();
        passwordJlabel.setBounds(155, 202, 67, 30);
        passwordTextField.setBounds(243, 202, 200, 30);
        getContentPane().add(passwordJlabel);
        getContentPane().add(passwordTextField);

        resetButton =new JButton("重置");
        resetButton.setBounds(340, 265, 90, 30);
        getContentPane().add(resetButton);
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                resetValue(ae);
            }
        });

        submitButton =new JButton("登录");
        submitButton.setBounds(195, 265, 90, 30);
        getContentPane().add(submitButton);
        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                loginAct(ae);
            }
        });

    }

    protected void loginAct(ActionEvent ae) {
        // TODO 自动生成的方法存根
        String aname=anameTextField.getText().toString();
        String password=passwordTextField.getText().toString();
        if(StringUtil.isEmpty(aname)){
            JOptionPane.showMessageDialog(this, "用户名不能为空！");
            return;
        }
        if(StringUtil.isEmpty(password)){
            JOptionPane.showMessageDialog(this, "密码不能为空！");
            return;
        }
        AdminDao adminDao = new AdminDao();
        Admin adminTmp = new Admin();
        adminTmp.setName(aname);
        adminTmp.setPassword(password);
        Admin admin = adminDao.login(adminTmp);
        adminDao.closeDao();
        if(admin == null){
            JOptionPane.showMessageDialog(this, "用户名或密码错误！");
            return;
        }
        JOptionPane.showMessageDialog(this, "欢迎"+admin.getName()+"登录本系统！");
        this.dispose();
        new MainFrm(admin).setVisible(true);
    }

    protected void resetValue(ActionEvent ae) {
        anameTextField.setText("");
        passwordTextField.setText("");
    }
}
